libxenlight: move logging macros to the public header
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 25 Nov 2009 14:19:50 +0000 (14:19 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 25 Nov 2009 14:19:50 +0000 (14:19 +0000)
This patch moves the logging macros to the public header so that they
can be reused by the client of the library.  It also refactors the
code to create the qemu logfile into a generic function that can be
reused to create generic xen logfiles under /var/log/xen.  Finally xl
is changed to log to file when running in background.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
tools/libxl/libxl.c
tools/libxl/libxl.h
tools/libxl/libxl_internal.h
tools/libxl/libxl_utils.c
tools/libxl/libxl_utils.h
tools/libxl/xl.c

index da2d1dfce1d71af8e610d0000c92216584c338ed..825363f1ddc27faeec62537d6fee701f06f5332a 100644 (file)
@@ -19,7 +19,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <sys/select.h>
@@ -765,10 +764,9 @@ int libxl_create_device_model(struct libxl_ctx *ctx,
                               libxl_device_nic *vifs, int num_vifs,
                               libxl_device_model_starting **starting_r)
 {
-    char *path, *logfile, *logfile_new;
-    struct stat stat_buf;
+    char *path, *logfile;
     int logfile_w, null;
-    int i, rc;
+    int rc;
     char **args;
     struct libxl_spawn_starting buf_spawn, *for_spawn;
 
@@ -789,22 +787,9 @@ int libxl_create_device_model(struct libxl_ctx *ctx,
     path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
     xs_mkdir(ctx->xsh, XBT_NULL, path);
 
-    logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
-    if (stat(logfile, &stat_buf) == 0) {
-        /* file exists, rotate */
-        logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", info->dom_name);
-        unlink(logfile);
-        for (i = 9; i > 0; i--) {
-            logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i);
-            logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i + 1);
-            rename(logfile, logfile_new);
-        }
-        logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
-        logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", info->dom_name);
-        rename(logfile, logfile_new);
-    }
-    logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
+    libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
     logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
+    free(logfile);
     null = open("/dev/null", O_RDONLY);
 
     if (starting_r) {
index b568bc48631a3dc78f4f7f2351a827c321e3a15c..4021b015f1c282236055a1743a9145b1edaa8945 100644 (file)
 
 #include "xen_uuid.h"
 
+#define XL_LOGGING_ENABLED
+
 typedef void (*libxl_log_callback)(void *userdata, int loglevel, const char *file,
                                    int line, const char *func, char *s);
-
 struct libxl_dominfo {
     xen_uuid_t uuid[16];
     uint32_t domid;
@@ -224,6 +225,25 @@ typedef struct  {
 #define ERROR_NOMEM (-1032)
 #define ERROR_INVAL (-1245)
 
+/* logging */
+void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
+void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
+
+#ifdef XL_LOGGING_ENABLED
+#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...)   xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
+#else
+#define XL_LOG(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
+#endif
+
+#define XL_LOG_DEBUG 3
+#define XL_LOG_INFO 2
+#define XL_LOG_WARNING 1
+#define XL_LOG_ERROR 0
+
 /* context functions */
 int libxl_ctx_init(struct libxl_ctx *ctx);
 int libxl_ctx_free(struct libxl_ctx *ctx);
index 8363c9453220f74c714169cd11b561156568a842..1c5a2de43d35bdb38c975134ebc8862d4f7321ec 100644 (file)
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
-
-#define XL_LOGGING_ENABLED
-
-#ifdef XL_LOGGING_ENABLED
-#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...)   xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
-#else
-#define XL_LOG(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
-#endif
-
-#define XL_LOG_DEBUG 3
-#define XL_LOG_INFO 2
-#define XL_LOG_WARNING 1
-#define XL_LOG_ERROR 0
-
-void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
-void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
-
 typedef enum {
     DEVICE_VIF,
     DEVICE_VBD,
index 358f735b4fe0e03933eae6fa6d0b95902d192892..b42bfc6991a7885fc5584c61d3349ffb96abe73d 100644 (file)
@@ -23,6 +23,9 @@
 #include <xenctrl.h>
 #include <ctype.h>
 #include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "libxl_utils.h"
 #include "libxl_internal.h"
@@ -187,3 +190,27 @@ int libxl_is_stubdom(struct libxl_ctx *ctx, int domid)
         return 0;
 }
 
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
+{
+    struct stat stat_buf;
+    char *logfile, *logfile_new;
+    int i;
+
+    logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+    if (stat(logfile, &stat_buf) == 0) {
+        /* file exists, rotate */
+        logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.10", name);
+        unlink(logfile);
+        for (i = 9; i > 0; i--) {
+            logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i);
+            logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i + 1);
+            rename(logfile, logfile_new);
+        }
+        logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+        logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name);
+        rename(logfile, logfile_new);
+    }
+    *full_name = strdup(logfile);
+    return 0;
+}
+
index e4e487cb23156010c1ba0c5e8fec50d0e418390e..cf57b0febb05e69f9dc92e28a3affb1b50d66de7 100644 (file)
@@ -31,6 +31,7 @@ char *libxl_uuid_to_string(struct libxl_ctx *ctx, xen_uuid_t *uuid);
 int libxl_param_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid);
 int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid);
 int libxl_is_stubdom(struct libxl_ctx *ctx, int domid);
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
 
 #endif
 
index 4c746609518815e31cd826fdaa386617dd5f6456..4056e13c45b36013506f3bc5450b029d63e770ca 100644 (file)
 #include "libxl.h"
 #include "libxl_utils.h"
 
+int logfile = 2;
+
 void log_callback(void *userdata, int loglevel, const char *file, int line, const char *func, char *s)
 {
-    fprintf(stderr, "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s);
+    char str[1024];
+
+    snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s);
+    write(logfile, str, strlen(str));
 }
 
 static void printf_info(libxl_domain_create_info *c_info,
@@ -640,9 +645,18 @@ start:
     libxl_domain_unpause(&ctx, domid);
 
     if (need_daemon) {
+        char *fullname, *name;
+
+        asprintf(&name, "xl-%s", info1.name);
+        libxl_create_logfile(&ctx, name, &fullname);
+        logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
+        free(fullname);
+        free(name);
+
         daemon(0, 0);
         need_daemon = 0;
     }
+    XL_LOG(&ctx, XL_LOG_DEBUG, "Waiting for domain %s (domid %d) to die", info1.name, domid);
     
     libxl_wait_for_domain_death(&ctx, domid, &fd);
     while (1) {
@@ -658,17 +672,23 @@ start:
         if (!ret)
             continue;
         if (libxl_is_domain_dead(&ctx, domid, &info)) {
+            XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d is dead", domid);
             if (info.crashed || info.dying || (info.shutdown && (info.shutdown_reason != SHUTDOWN_suspend))) {
+                XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d needs to be clean: destroying the domain", domid);
                 libxl_domain_destroy(&ctx, domid, 0);
                 if (info.shutdown && (info.shutdown_reason == SHUTDOWN_reboot)) {
                     libxl_ctx_free(&ctx);
+                    XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Rebooting now");
                     goto start;
                 }
+                XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Exiting now");
             }
+            XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d does not need to be clean, exiting now", domid);
             exit(0);
         }
     }
 
+    close(logfile);
     for (i = 0; i < num_vifs; i++) {
         free(vifs[i].smac);
         free(vifs[i].ifname);